package es.udc.emarketapp.emarket.model.product.dao;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;

import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.sql.GeneralOperations;
import es.udc.emarketapp.emarket.model.product.vo.ProductVO;


// An implementation of <code>SQLProductDAO</code> for databases providing
// counter columns with JDBC 3 drivers.
 

public class MySQLProductDAO extends AbstractSQLProductDAO {

    public ProductVO create(Connection connection, ProductVO productVO) 
        throws InternalErrorException {
        
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
    
        try {
                
            // Create "preparedStatement". 
            String queryString = "INSERT INTO producto" +
                "(id_vendedor, id_cat, nom_prod, descripcion, pais_origen," +
                "uds_disp, imagen, precio, peso, valido)" +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        
            preparedStatement = connection.prepareStatement(queryString,
                Statement.RETURN_GENERATED_KEYS);    
            
            // Fill "preparedStatement". 
            
            preparedStatement.setString(1, productVO.getIdVendedor());
            preparedStatement.setLong(2, productVO.getIdCat().longValue());
            preparedStatement.setString(3, productVO.getNomProd());
            preparedStatement.setString(4, productVO.getDescripcion());
            preparedStatement.setString(5, productVO.getPaisOrigen());
            preparedStatement.setLong(6, productVO.getUdsDisp().longValue());
            preparedStatement.setBytes(7 , productVO.getImagen());
            preparedStatement.setDouble(8, productVO.getPrecio());
            preparedStatement.setDouble(9, productVO.getPeso());
            preparedStatement.setBoolean(10, productVO.isValido());
                        
            // Execute query.
            int insertedRows = preparedStatement.executeUpdate();
        
            if (insertedRows == 0) {
                throw new SQLException("No se pudo insertar la fila a la tabla" +
                    " 'Producto'");
            }

            if (insertedRows > 1) {
                throw new SQLException("Fila duplicada en la tabla 'Producto'");
            }
            
            // Get account identifier. 
            resultSet = preparedStatement.getGeneratedKeys();

            if (!resultSet.next()) {
                throw new InternalErrorException(new SQLException(
                    "el driver JDBC no ha devuelto el identificador generado"));
            }
            Long idProd = new Long(resultSet.getLong(1)); 
            
            // Return the value object. 
            return new ProductVO(idProd.longValue(), productVO.getIdVendedor(), 
            		productVO.getIdCat().longValue(), productVO.getNomProd(), 
            		productVO.getDescripcion(), productVO.getPaisOrigen(),
            		productVO.getUdsDisp().longValue(),productVO.getImagen(),
            		productVO.getPrecio(), productVO.getPeso(), 
            		productVO.isValido());
            
        
        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeStatement(preparedStatement);
        }
        
    }
}